package view;

import java.awt.Graphics;
import java.awt.KeyboardFocusManager;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.text.Document;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.StyledEditorKit.StyledTextAction;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import layout.TableLayout;

import com.hexidec.util.Translatrix;

/**
 * This class extends JPanel and behaves like a WYSIWYG Editor.
 * @author sloth42
 *
 */
public class EditCardPanel extends JPanel {

	private HTMLDocument docLeft = null;
	private HTMLDocument docRight = null;
	private JTextPane paneLeft = new JTextPane();
	private JTextPane paneRight = new JTextPane();
	private JSplitPane splitPane = new JSplitPane();
	private JToolBar toolbar = new JToolBar();
	private JButton btnBold = new JButton();
	private JButton btnItalic = new JButton();
	private JButton btnUnderline = new JButton();
	private JLabel lblFont = new JLabel(Translatrix.getTranslationString("frontSide"));
	private JLabel lblBack = new JLabel(Translatrix.getTranslationString("backSide"));

	/**
	 * This Class represents a Panel which you can use to manipulate Cards.
	 * @param docLeft the front side of a card
	 * @param docRight the reverse side of a card
	 */
	public EditCardPanel(HTMLDocument docLeft, HTMLDocument docRight) {
		super();
		this.docLeft = docLeft;
		this.docRight = docRight;
		paneLeft.setText("<html></html>");

		initSpiltpane();
		initToolbar();

		double border = 5;
		double size[][] = {
				{ border, TableLayout.FILL, TableLayout.FILL, border }, // Columns
				{ border, 55, 15, TableLayout.FILL, border } }; // Rows

		this.setLayout(new TableLayout(size));
		// c r c r
		this.add(toolbar, "1,1, 2,1");
		this.add(lblFont, "1,2");
		this.add(lblBack, "2,2");
		this.add(splitPane, "1,3, 2,4");

		this.setVisible(true);
	}

	private void initSpiltpane() {

		HTMLEditorKit ek = new HTMLEditorKit();

		if (docLeft == null || docRight == null) {
			docLeft = (HTMLDocument) ek.createDefaultDocument();
			docRight = (HTMLDocument) ek.createDefaultDocument();
		}
		paneLeft.setContentType("text/html");
		paneLeft.setDocument(docLeft);

		paneRight.setContentType("text/html");
		paneRight.setDocument(docRight);

		ek.install(paneLeft);
		ek.install(paneRight);
		
		//changes behavior of tab key to navigate through the components
		paneLeft.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
        paneLeft.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);

        paneRight.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
        paneRight.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);

		splitPane.setLeftComponent(paneLeft);
		splitPane.setRightComponent(paneRight);
	}

	private void initToolbar() {
		StyledTextAction bold = new StyledEditorKit.BoldAction();
		StyledTextAction italic = new StyledEditorKit.ItalicAction();
		StyledTextAction underline = new StyledEditorKit.UnderlineAction();

		btnBold.setAction(bold);
		btnItalic.setAction(italic);
		btnUnderline.setAction(underline);

		btnBold.setText("");
		btnBold.setIcon(new ImageIcon("src/img/format-text-bold.png"));
		btnItalic.setText("");
		btnItalic.setIcon(new ImageIcon("src/img/format-text-italic.png"));
		btnUnderline.setText("");
		btnUnderline
				.setIcon(new ImageIcon("src/img/format-text-underline.png"));

		toolbar.add(btnBold);
		toolbar.add(btnItalic);
		toolbar.add(btnUnderline);
	}

	public String getFrontSideText() {
		return paneLeft.getText();
	}
	
	public Document getFrontSide() {
		return paneLeft.getDocument();
	}
	
	public Document getReverseSide() {
		return paneRight.getDocument();
	}
	
	public void updateDivider() {
		splitPane.setDividerLocation(0.5);
	}
	

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		updateDivider();
	}

}